package com.pg_chat.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

@Component
public class WordBand {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static Node root = new Node();

     public WordBand(){
        try {
            FileInputStream fIS = null;
    //        StringBuffer buffer= new StringBuffer();
            BufferedReader buffer = null;
            String line = new String();
            StringBuffer result = new StringBuffer();

                fIS = new FileInputStream("src/main/resources/wordForbidden/ban.txt");
    //            int b;
    //
    //            while ((b= fIS.read())!= -1){
    //                buffer.append((char)b);
    //            }
                buffer = new BufferedReader(new InputStreamReader(fIS));
                while ((line = buffer.readLine())!=null){
                    result.append(line);

                }
            String content = result.toString();
            String[] words = content.split("\\|");

            for (String word : words) {
                addWord(word);
            }
            buffer.close();
            fIS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //添加敏感词
    public void addWord(String word){
        Node tmpNode = root;
        for (Character c:word.toCharArray()){
            Node node = tmpNode.getNext(c);
            if(null == node){
                node = new Node();
                tmpNode.addNext(c, node);
            }
            tmpNode = node;
        }
    }
    //敏感词替换
    public String replace(String text, String afterReplace){
        StringBuffer result = new StringBuffer(text.length());
        Node tmpNode = root;
        int begin = 0, pos = 0;
        while (pos<text.length()){
            Character c = text.charAt(pos);
            tmpNode = tmpNode.getNext(c);
            if(null == tmpNode){
                result.append(text.charAt(begin));
                begin++;
                pos = begin;
                tmpNode = root;
            }else if (tmpNode.isLastCharacter()){
                result.append(afterReplace);
                pos++;
                begin = pos;
                tmpNode = root;
            }else {
                pos++;
            }
        }
        result.append(text.substring(begin));
        return result.toString();
    }
    public Map<String, Integer> find(String text) {
        Map<String, Integer> resultMap = new HashMap<>(16);
        Node tmpNode = root;
        StringBuilder word = new StringBuilder();
        int begin = 0, pos = 0;
        while (pos < text.length()) {
            Character c = text.charAt(pos);
            tmpNode = tmpNode.getNext(c);
            if (null == tmpNode) {
                begin++;
                pos = begin;
                tmpNode = root;
            } else if (tmpNode.isLastCharacter()) {
                // 匹配完成
                String w = word.append(c).toString();
                resultMap.put(w, resultMap.getOrDefault(w, 0) + 1);
                pos++;
                begin = pos;
                tmpNode = root;
                word = new StringBuilder();
            } else {
                // 匹配上向后移
                word.append(c);
                pos++;
            }
        }
        return resultMap;
    }



    //节点
    static class Node{
        private Map<Character,Node> nextNode = new HashMap<>();

        public void addNext(Character key, Node node){
            nextNode.put(key,node);
        }

        public Node getNext(Character key){
            return nextNode.get(key);
        }

        public boolean isLastCharacter(){
            return nextNode.isEmpty();
        }
    }

    public static void main(String[] args){
        WordBand wordBand = new WordBand();
        System.out.println(wordBand.replace(args[0],args[1] ));
    }
}
